package study.activiti.m_groupUser;

import java.io.InputStream;
import java.util.List;

import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.impl.persistence.entity.GroupEntity;
import org.activiti.engine.impl.persistence.entity.UserEntity;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

/**  
 * @Name: GroupUserTest
 * @Description: 用户角色组测试类
 * @Author: 李超
 * @Create Date: 2016-12-16
 */
public class GroupUserTest {
	
	//流程引擎
	ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine() ;
	
	//部署流程定义，创建用户角色组
	@Test
	public void depolymentProcessDefinitionAndCreateGroupUsers() {
		//部署流程定义
		InputStream inputStreamBpmn = this.getClass().getResourceAsStream("groupUser.bpmn") ;
		InputStream inputStreamPng = this.getClass().getResourceAsStream("groupUser.png") ;
		processEngine.getRepositoryService()
					 .createDeployment()
					 .name("用户角色组") 
					 .addInputStream("groupUser.bpmn", inputStreamBpmn)
					 .addInputStream("groupUser.png", inputStreamPng)
					 .deploy() ;
		//添加用户角色组
		IdentityService identityService = processEngine.getIdentityService() ;
		//创建角色
		identityService.saveGroup(new GroupEntity("部门经理")) ;
		identityService.saveGroup(new GroupEntity("总经理")) ;
		//创建用户
		identityService.saveUser(new UserEntity("张三")) ;
		identityService.saveUser(new UserEntity("李四")) ;
		identityService.saveUser(new UserEntity("王五")) ;
		//建立用户和角色的关系
		identityService.createMembership("张三", "部门经理") ;
		identityService.createMembership("李四", "部门经理") ;
		identityService.createMembership("王五", "总经理") ;
		System.out.println("添加组织机构成功");
	}
	
	//启动流程实例
	@Test
	public void startProcessInstance() {
		//流程定义的key
		String processDefinitionKey = "groupUser" ;
		//启动流程实例
		ProcessInstance pi = processEngine.getRuntimeService().startProcessInstanceByKey(processDefinitionKey) ;
		if(pi != null) {
			System.out.println("流程启动成功");
			System.out.println("流程实例id:" + pi.getProcessInstanceId());
		}
	}
	
	//查询候选组的组任务
	@Test
	public void queryGroupTaskByCandidateGroupUser() {
		//候选组
		//String candidateGroup = "部门经理" ;
		//候选组中的用户
		String candidateGroupUser = "张三" ;
		//查询组任务
		List<Task> list = processEngine.getTaskService()
					 .createTaskQuery()
//					 .taskCandidateGroup(candidateGroup) //根据候选组查询
					 .taskCandidateUser(candidateGroupUser) //根据候选组中的用户查询
					 .orderByTaskCreateTime().asc()
					 .list() ;
		if(list != null && list.size() > 0) {
			for (Task task : list) {
				System.out.println("任务id:" + task.getId());
				System.out.println("任务名称:" + task.getName());
				System.out.println("任务办理人:" + task.getAssignee());
				System.out.println("任务创建时间:" + task.getCreateTime());
				System.out.println("流程实例id:" + task.getProcessInstanceId());
				System.out.println("执行对象id:" + task.getExecutionId());
				System.out.println("######################################");
			}
		}
	}
	
	//拾取任务：为组任务指定办理人，转为个人任务
	@Test
	public void claim() {
		//任务id
		String taskId = "247504" ;
		//任务办理人：这里指定为候选组-部门经理中的用户
		String candidateGroupUser = "张三" ;
		//为组任务指定办理人
		processEngine.getTaskService().claim(taskId, candidateGroupUser) ;
	}
	
	//查询个人任务
	@Test
	public void queryMyPersonalTask() {
		//任务办理人
		String assignee = "张三" ;
		//查询个人任务
		Task task = processEngine.getTaskService().createTaskQuery().taskAssignee(assignee).singleResult() ;
		if(task != null) {
			System.out.println("任务id:" + task.getId());
			System.out.println("任务名称:" + task.getName());
			System.out.println("任务办理人:" + task.getAssignee());
			System.out.println("任务创建时间:" + task.getCreateTime());
			System.out.println("流程实例id:" + task.getProcessInstanceId());
			System.out.println("执行对象id:" + task.getExecutionId());
			System.out.println("######################################");
		}
	}
	
	//办理任务
	@Test
	public void completeTask() {
		//任务id
		String taskId = "247504" ;
		//办理任务
		processEngine.getTaskService().complete(taskId) ;
		System.out.println("完成任务");
	}
	

}
